import os
import cv2
import sys
#https://www.yisu.com/zixun/690257.html

def blurImageDetection(file_path,thres=0.07):
	# 读取影像为灰度图
	img = cv2.imread(file_path,0)
	#gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
	height, width = img.shape
	# 缩小影像，加快处理速度
	if(width > 1920 or height > 1080):
		tiny_img = cv2.resize(img, (1920, 1080), fx=0, fy=0)
		# 获取影像尺寸
		height, width = tiny_img.shape
	else:
		tiny_img = img
	# 计算影像的模糊程度
	blurness = cv2.Laplacian(tiny_img, cv2.CV_64F).var() / (width * height)
	# 如果影像模糊程度小于阈值就将其移动到存放模糊影像的文件夹中
	if blurness < thres:
		print(file_path + "  bulrness:%f   模糊" % (blurness))
		#blurImagePath = os.path.join(blurImageDirPath, imageName)
		#shutil.move(imagePath, blurImagePath)
	else:
		print(file_path + "  blurness:%f   不模糊" % (blurness))
	#for i in range(1,100)
	# cv2.imshow('result', img[:100,:200])
	# cv2.waitKey(0)
	# cv2.destroyAllWindows()
	height, width = img.shape
	partNum = 50
	dy = height/partNum
	dx = width/partNum
	for i in range(partNum):
		for j in range(partNum):
			#print("[%d:%d,%d:%d]="%(int(i*dy),int((i+1)*dy),int(j*dx),int((j+1)*dx)),end="")
			pimg = img[int(i*dy):int((i+1)*dy),int(j*dx):int((j+1)*dx)]
			# if(i==0 and j==15):
			# 	cv2.imwrite("d://"+str(i)+"_"+str(j)+".jpg",pimg)
			blurness = cv2.Laplacian(pimg, cv2.CV_64F).var() / (dx * dy)
			if(blurness<0.01):
				cv2.rectangle(img, (int(i*dx), int(j*dy)), (int((i+1)*dx),int((j+1)*dy)), (0, 0, 0), thickness=-1)
			print(blurness,end=",")
		print()
	# cv2.imshow('result', img)
	# cv2.rectangle(img, (200, 250), (300,350), (0, 0, 0), thickness=-1)
	# cv2.line(img, (0, 0), (300, 350), (255, 0, 0), 5)  # 画蓝色背景线
	cv2.imwrite('blurness.png', img)
if __name__ == '__main__':
    #指定要处理的文件名，sys.argv[1]为第一个参数
    #file_path = os.getcwd()+'/'+sys.argv[1]
	file_path = sys.argv[1]
	#调用函数
	blurImageDetection(file_path)